﻿package com.cenfun.ui{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.media.Video;

	public class BS extends UI {
		
		protected var main:Sprite;
		protected var sb:SB;
		
		private var _content:DisplayObject;
		
		public function BS():void {
			tw = 100;
			th = 100;
			
			main = new Sprite();
			main.cacheAsBitmap = true;
			addChild(main);
			
			sb = new SB();
			sb.addEventListener(SB.CHANGE, scrollHandler);
			addChild(sb);
			
			layout();
			
			addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel, false, 0, true);
		}
		
		protected function mouseWheel(e:MouseEvent):void {
			if (!sb.visible) {
				return;
			}
			sb.scrollPosition -= e.delta * sb.scrollSize;
		}
		
		
		private function scrollHandler(e:UE):void {
			var per:Number = Number(e.data);
			
			if (content) {
				content.y = - Math.round((content.height - th) * per);
			}
			
		}
		
		public function update():void {
			
			if (content) {
				sb.visible = true;
				sb.update(content.y, content.height);
			} else {
				sb.visible = false;
			}
			
		}
		
		
		public function set content(source:DisplayObject):void {
			if (content != source) {
				if (content && main.contains(content)) {
					main.removeChild(content);
				}
				if (source is DisplayObject) {
					main.addChild(source);
					_content = source;
				}
				update();
			}
			
			
		}
		public function get content():DisplayObject {
			return _content;
		}

		
		
		override public function layout():void {
			sb.move(tw - sb.width, 0);
			sb.height = th;
			update();
			
			var rect:Rectangle = new Rectangle(0, 0, tw, th);
			if (sb.visible) {
				rect = new Rectangle(0, 0, tw - sb.width, th);
			}
			scrollRect = rect;
		}
		
		
	}
}